<?php

namespace app\tools;

use think\cache\driver\Redis;

/**
 * 百度云API接口
 *
 * user ：Du
 * Date : 2022/2/24
 * Time : 9:35
 * Class Baidu
 * projects\tools
 */
class Baidu
{

    public static string $tokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';

    /**
     * 银行卡识别
     *
     * User : Du
     * Date : 2022/2/24
     * Time : 9:33
     * @param array $data
     * @return bool|string
     */
    public static function bankcard(array $data)
    {
        $token = self::getAccessToken();
        if (!$token) {
            return '获取token失败';
        }
        $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?access_token=' . $token;
        $arr = [
            'url' => $data['url'],
            'detect_direction' => false
        ];
        $res = self::request_post($url, $arr);
        $res = jsonToArray($res);
        if (!$res['result'] || $res['result']['bank_card_number'] === '') {
            return '未能识别该银行卡';
        }
        switch ($res['result']['bank_card_type']) {
            case 1:
                $text = '借记卡';
                break;
            case 2:
                $text = '贷记卡';
                break;
            case 3:
                $text = '准贷记卡';
                break;
            case 4:
                $text = '预付费卡';
                break;
            default:
                $text = '不能识别';
        }
        $res['result']['type_name'] = $text;
        return $res;
    }

    /**
     * 获取百度token
     *
     * User : Du
     * Date : 2021/12/17
     * Time : 11:13
     * @return false|mixed
     */
    private static function getAccessToken()
    {
        $redis = new Redis();
        if (!$accessToken = $redis->get('baiDuAccessToken')) {
//            $api_key = env('baidu.apikey');
//            $secret_key = env('baidu.secretkey');
            $api_key = 'eqMf7AOVWbvIyZGMzjKuDdVy';
            $secret_key = 'iBQxNP8cWDNOIN30mG5xUKepBXqnGZsS';
            $data = [
                'grant_type' => 'client_credentials',
                'client_id' => $api_key,
                'client_secret' => $secret_key,
            ];
            $o = "";
            foreach ($data as $k => $v) {
                $o .= "$k=" . urlencode($v) . "&";
            }
            $data = substr($o, 0, -1);
            $res = self::request_post(self::$tokenUrl, $data);
            if (!isset($res['access_token'])) {
                return false;
            }
            $redis->set('baiDuAccessToken', $res['access_token'], 86400);
            $accessToken = $res['access_token'];
        }
        return $accessToken;
    }

    /**
     * 模拟请求
     *
     * User : Du
     * Date : 2021/12/17
     * Time : 16:37
     * @param  $url
     * @param  $param
     * @return bool|string
     */
    private static function request_post($url, $param)
    {
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }
}